package com.mdata.common;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class BaseRestRequest {

    public static <T> T getForObject(RestTemplate restTemplate, String url, Class<T> responseType){
        return getForObject(restTemplate, url, new HashMap<>(), null, responseType);
    }

    public static <T> T getForObject(RestTemplate restTemplate, String url, MultiValueMap<String, String> queryParamMap, Class<T> responseType){
        return getForObject(restTemplate, url, new HashMap<>(), queryParamMap, responseType);
    }

    public static <T> T getForObject(RestTemplate restTemplate, String url, Map<String, Object> uriVariableMap, MultiValueMap<String, String> queryParamMap, Class<T> responseType){
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(queryParamMap);
        return restTemplate.getForObject(builder.toUriString(), responseType, uriVariableMap);
    }

    public static <T> T postForObject(RestTemplate restTemplate, String url, Class<T> responseType){
        return postForObject(restTemplate, url, new HashMap<>(), null, responseType);
    }

    public static <T> T postForObject(RestTemplate restTemplate, String url, MultiValueMap<String, String> kvMap, Class<T> responseType){
        return postForObject(restTemplate, url, new HashMap<>(), kvMap, responseType);
    }

    public static <T> T postForObject(RestTemplate restTemplate, String url, Map<String, Object> uriVariableMap, MultiValueMap<String, String> kvMap, Class<T> responseType) {
        if(Objects.nonNull(kvMap)){
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
            HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(kvMap, httpHeaders);
            return restTemplate.postForObject(url, httpEntity, responseType, uriVariableMap);
        }
        return restTemplate.postForObject(url, null, responseType, uriVariableMap);
    }

    public static <T> T postForObject(RestTemplate restTemplate, String url, Object object, Class<T> responseType) {
        return postForObject(restTemplate, url, new HashMap<>(), object, responseType);
    }

    public static <T> T postForObject(RestTemplate restTemplate, String url,Map<String, Object> uriVariableMap, Object object, Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<Object> httpEntity = new HttpEntity<>(object,httpHeaders);
        return restTemplate.postForObject(url, httpEntity, responseType, uriVariableMap);
    }
}
